package com.english.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.english.entity.AttendanceLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 签到签退日志数据访问层
 *
 * @author wlq
 * @since 2025-08-08
 */
@Mapper
public interface AttendanceLogMapper extends BaseMapper<AttendanceLog> {

    /**
     * 根据学生ID查询签到签退日志
     *
     * @param studentId 学生ID
     * @return 日志列表
     */
    @Select("SELECT al.*, u1.name as studentName, u2.name as adminName " +
            "FROM attendance_log al " +
            "LEFT JOIN user u1 ON al.student_id = u1.id " +
            "LEFT JOIN user u2 ON al.admin_id = u2.id " +
            "WHERE al.student_id = #{studentId} " +
            "ORDER BY al.action_time DESC")
    List<AttendanceLog> getLogsByStudentId(@Param("studentId") Long studentId);

    /**
     * 根据日期范围查询签到签退日志
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 日志列表
     */
    @Select("SELECT al.*, u1.name as studentName, u2.name as adminName " +
            "FROM attendance_log al " +
            "LEFT JOIN user u1 ON al.student_id = u1.id " +
            "LEFT JOIN user u2 ON al.admin_id = u2.id " +
            "WHERE DATE(al.action_time) BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY al.action_time DESC")
    List<AttendanceLog> getLogsByDateRange(@Param("startDate") LocalDate startDate, 
                                          @Param("endDate") LocalDate endDate);

    /**
     * 查询今日签到签退日志
     *
     * @param date 日期
     * @return 日志列表
     */
    @Select("SELECT al.*, u1.name as studentName, u2.name as adminName " +
            "FROM attendance_log al " +
            "LEFT JOIN user u1 ON al.student_id = u1.id " +
            "LEFT JOIN user u2 ON al.admin_id = u2.id " +
            "WHERE DATE(al.action_time) = #{date} " +
            "ORDER BY al.action_time DESC")
    List<AttendanceLog> getTodayLogs(@Param("date") LocalDate date);

    /**
     * 查询最近的操作日志
     *
     * @param limit 限制条数
     * @return 日志列表
     */
    @Select("SELECT al.*, u1.name as studentName, u2.name as adminName " +
            "FROM attendance_log al " +
            "LEFT JOIN user u1 ON al.student_id = u1.id " +
            "LEFT JOIN user u2 ON al.admin_id = u2.id " +
            "ORDER BY al.action_time DESC " +
            "LIMIT #{limit}")
    List<AttendanceLog> getRecentLogs(@Param("limit") Integer limit);
} 